<template>
  <el-row class="home" :gutter="20">
    <!-- 左边栏 -->
    <el-col :span="8" style="margin-top: 20px">
      <!-- 用户信息区域 -->
      <el-card shadow="hover">
        <div class="user clearfix" slot="header">
          <img :src="userImg" alt="用户头像" />
          <div class="userinfo">
            <p class="username">admin</p>
            <p class="useraccess">超级管理员</p>
          </div>
        </div>
        <div class="logininfo">
          <p>上次登录时间：<span>2021-7-19</span></p>
          <p>上次登录地点：<span>武汉</span></p>
        </div>
      </el-card>
      <!-- 商品购买详情区域 -->
      <el-card style="height: 460px; margin-top: 20px" shadow="hover">
        <el-table :data="tableData" style="width: 100%">
          <el-table-column
            v-for="(val, index) in tableLabel"
            :key="index"
            :prop="index"
            :label="val"
          ></el-table-column>
        </el-table>
      </el-card>
    </el-col>

    <!-- 右边栏 -->
    <el-col :span="16" style="margin-top: 20px">
      <!-- 站点指标区域 -->
      <div class="indicator">
        <el-card
          v-for="item in countData"
          :key="item.name"
          :body-style="{ display: 'flex', padding: 0 }"
        >
          <i
            class="icon"
            :class="`el-icon-${item.icon}`"
            :style="{ background: item.color }"
          ></i>
          <div class="detail">
            <p class="num">￥{{ item.value }}</p>
            <p class="txt">{{ item.name }}</p>
          </div>
        </el-card>
      </div>
      <!-- order折线图区域 -->
      <el-card style="height: 280px; margin-bottom: 20px">
        <echart-div :chartData="eChartData.order" style="height: 280px" />
      </el-card>
      <div class="graph">
        <!-- user柱状图区域 -->
        <el-card style="height: 260px; width: 49%">
          <echart-div :chartData="eChartData.user" style="height: 240px" />
        </el-card>
        <!-- video饼图区域 -->
        <el-card style="height: 260px; width: 49%">
          <echart-div :chartData="eChartData.video" :isAxisChart="false" style="height: 240px" />
        </el-card>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import { getData } from "@/api/data.js";
import EchartDiv from '@/components/EchartDiv'

export default {
  name: "HomeView",
  components: {
    EchartDiv
  },
  data() {
    return {
      userImg: require("@/assets/img/logo.png"),
      tableLabel: {
        name: "课程",
        todayBuy: "今日购买",
        monthBuy: "本月购买",
        totalBuy: "累计购买",
      },
      tableData: [],
      countData: [],
      eChartData: {
        order: {},
        user: {},
        video: {}
      }
    };
  },
  mounted() {
    getData().then((res) => {
      const { code, data } = res.data;
      if (code === 20000) {
        this.tableData = data.tableData;
        this.countData = data.countData;

        // 销量折线图
        const order = data.orderData;
        const orderKeyArray = Object.keys(order.data[0]);
        const orderSeries = [];
        orderKeyArray.forEach((key) => {
          orderSeries.push({
            name: key,
            data: order.data.map((item) => item[key]),
            type: "line",
          });
        });
        this.eChartData.order = {
          xData: order.date,
          series: orderSeries
        }

        // 用户量柱状图
        this.eChartData.user = {
          xData: data.userData.map((item) => item.date),
          series: [
            {
              name: "新增用户",
              data: data.userData.map((item) => item.new),
              type: 'bar'
            },
            {
              name: "活跃用户",
              data: data.userData.map((item) => item.active),
              type: 'bar'
            },
          ],
        }
        
        // video饼图
        this.eChartData.video = {
          series: [
            {
              data: data.videoData,
              type: 'pie'
            }
          ]
        }
      }
    });
  },
};
</script>

<style lang="less" scoped>
.home {
  .user {
    display: flex;
    justify-content: flex-start;
    flex-wrap: wrap;
    align-items: center;
    img {
      width: 180px;
      height: 180px;
      border-radius: 50%;
      border-style: solid;
      border-width: 1px;
      border-color: #98bf21;
    }
    .userinfo {
      margin-top: 20px;
      margin-left: 20px;
      text-align: left;
    }
  }
  .logininfo {
    text-align: left;
  }

  .indicator {
    display: flex;
    flex-flow: wrap;
    justify-content: flex-start;
    .el-card {
      width: 280px;
      display: flex;
      margin-right: 20px;
      margin-bottom: 20px;
      .icon {
        width: 84px;
        height: 100%;
        font-size: 48px;
        display: flex;
        align-items: center;
        justify-content: center;
      }
      .detail {
        margin-left: 10px;
        text-align: left;
        .num {
          font-size: 32px;
        }
        .txt {
          font-size: 12px;
        }
      }
    }
  }

  .graph {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
}
</style>